package org.sunrise.game.game.human;

import com.google.protobuf.Message;
import lombok.Getter;
import lombok.Setter;
import org.sunrise.game.common.db.entity.EntityHumanList;
import org.sunrise.game.common.log.LogCore;
import org.sunrise.game.common.message.GameMessage;
import org.sunrise.game.common.network.client.BaseClientManager;
import org.sunrise.game.common.utils.Utils;
import org.sunrise.game.game.proto.HumanProto;
import org.sunrise.game.game.proto.LoginProto;
import org.sunrise.game.game.proto.TopicProto;
import org.sunrise.game.game.server.GameManager;

import java.util.List;

@Getter
@Setter
public class ConnectObject {
    private final long connectId;
    private final int gameId; // game服id
    private final int externalId; // 对外服id
    private final String uid;
    private int accountId;


    public ConnectObject(long connectId, String uid) {
        this.gameId = GameManager.getGameId();
        this.connectId = connectId;
        this.uid = uid;
        this.externalId = (int) (connectId / Utils.ID_BASE_NUM);
    }

    public void sendMsg(int packetType, int packetId) {
        sendMsg(packetType, packetId, null);
    }

    public void sendMsg(int packetType, int packetId, Message.Builder builder) {
        var sendBuilder = TopicProto.MBasePacketData.newBuilder().setPacketType(TopicProto.TOPIC.forNumber(packetType)).setPacketId(packetId);
        if (builder != null) {
            sendBuilder.setPacketData(builder.build().toByteString());
        }
        var message = new GameMessage(connectId, gameId, sendBuilder.build().toByteArray());
        message.setNodeId(GameManager.getConnectToExternalNodeIdById(externalId));
        message.setFrom(Utils.FROM_GAME);
        BaseClientManager.sendToServer(message);

        LogCore.GameServer.debug("send msg, cur NodeId = { {} }, messageId = { {} }, connectionId = {}, uid = {}, packetType = {}, packetId = {}, msgData = {{}}", message.getNodeId(), message.getMessageId(), connectId, uid, packetType, packetId, builder == null ? "" : builder.toString().replace("\n", ""));
    }

    public void onLoadAccount(long accountId) {
        this.accountId = (int) accountId;
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_LOGIN_VALUE, LoginProto.FROM_SERVER.S2C_Login_VALUE, LoginProto.MS2C_Login.newBuilder().setAccountId(accountId));
    }

    public void onLoadHumanList(List<EntityHumanList> humanLists) {
        LoginProto.MS2C_HumanList.Builder builder = LoginProto.MS2C_HumanList.newBuilder();
        for (EntityHumanList entityInfo : humanLists) {
            LoginProto.STHumanShowInfo.Builder humanBuilder = LoginProto.STHumanShowInfo.newBuilder();
            humanBuilder.setHumanId(entityInfo.getHuman_id());
            humanBuilder.setName(entityInfo.getName());
            humanBuilder.setLevel(entityInfo.getLevel());
            humanBuilder.setPos(entityInfo.getPos());
            humanBuilder.setServerId(entityInfo.getServer_id());

            builder.addHumanList(humanBuilder);
        }
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_LOGIN_VALUE, LoginProto.FROM_SERVER.S2C_HumanList_VALUE, builder);
    }

    public void onSelectHuman() {
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_LOGIN_VALUE, LoginProto.FROM_SERVER.S2C_SelectHuman_VALUE);
        LogCore.GameServer.info("uid = { {} }, login success", uid);
    }

    public void onSendHumanData(HumanProto.MS2C_HumanInfo.Builder builder) {
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_HUMAN_VALUE, HumanProto.FROM_SERVER.S2C_HumanInfo_VALUE, builder);
    }

    public void onSendHumanDataEnd() {
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_HUMAN_VALUE, HumanProto.FROM_SERVER.S2C_SendInfoEnd_VALUE);
    }

    public void kick(String reason) {
        sendMsg(TopicProto.TOPIC.TOPIC_TYPE_LOGIN_VALUE, LoginProto.FROM_SERVER.S2C_Kick_VALUE, LoginProto.MS2C_Kick.newBuilder().setReason(reason));
        LogCore.GameServer.info("uid = { {} }, kick, reason = { {} }", uid, reason);
    }
}
